package com.cloudpivot.receivales.ding.basic.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.basic.service.DingBasicService;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiCallBackGetCallBackRequest;
import com.dingtalk.api.request.OapiCallBackRegisterCallBackRequest;
import com.dingtalk.api.response.OapiCallBackGetCallBackResponse;
import com.dingtalk.api.response.OapiCallBackRegisterCallBackResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

/**
 * 钉钉基础 api
 * 钉钉注册回调接口 场景群应用
 */
@Controller
@Slf4j
@RequestMapping("/saleapi/dingbasic")
public class DingBasicController {

    @Value("${dingcallback.token}")
    private String TOKEN;
    @Value("${dingcallback.aes}")
    private String AES;
    @Value("${dingcallback.url}")
    private String URL;



    @Autowired
    private DingBasicService dingBasicService;


    /**
     * 注册 钉钉回调服务
     *  对外接口
     */
    @PostMapping("/registerDingCallBack")
    @ResponseBody
    public OapiCallBackRegisterCallBackResponse registerCallBack(){
        String accessToken = dingBasicService.getToken("dinggroup");
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/register_call_back");
        OapiCallBackRegisterCallBackRequest request = new OapiCallBackRegisterCallBackRequest();
        request.setUrl(URL);
        request.setAesKey(AES);
        request.setToken(TOKEN);
        request.setCallBackTag(Arrays.asList("chat_add_member", "chat_remove_member", "chat_quit","chat_update_owner","chat_update_title","chat_disband"));

        try {
            OapiCallBackRegisterCallBackResponse rsp = client.execute(request,accessToken);
            log.info("回调注册返回rsp{}", JSONObject.toJSONString(rsp));
            return rsp;
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取钉钉已经配置的回调信息(测试、验证使用)
     */
    @PostMapping("/getDingCallBack")
    @ResponseBody
    public void getCallBack(){
        String accessToken = dingBasicService.getToken("dinggroup");
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/get_call_back");
        OapiCallBackGetCallBackRequest req = new OapiCallBackGetCallBackRequest();
        req.setHttpMethod("GET");
        try {
            OapiCallBackGetCallBackResponse rsp = client.execute(req, accessToken);
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }
}
